package persistance;

import java.util.*;

import org.jdom.Element;

public class UMLSequenceModeler 
{
		//no need for constructor yet..
	
		//an array list to hold a list of agents and their roles
		private ArrayList<Agent> _theAgentList;
		
		//an array list to hold a list of sorted messages associated with a file
		private ArrayList<Protocols> _theProtocolMessageList;
				
		
		//prints the list of agents
		public void print_theAgentList()
		{
			Iterator iterator = _theAgentList.iterator();
			
			System.out.println("\nLIST OF Agents:");
					
			while(iterator.hasNext())
			{
				Object o = iterator.next();
				if(o instanceof Agent)
				{
					Agent a = (Agent)o;
					System.out.println("AGENT: " + a.get_name() + " | ROLES: " + a.get_roles());		
				}							
			}						
		}
		
		//print the list of messages
		public void print_theProtocolMessageList()
		{
			Iterator iterator = _theProtocolMessageList.iterator();
			
			System.out.println("\nLIST OF MESSAGES FOR EACH FILENAME:");
			
			while(iterator.hasNext())
			{
				Object o = iterator.next();
				if(o instanceof Protocols)
				{
					Protocols p = (Protocols)o;
					
					System.out.println("FILENAME:" + p.get_filename());
					p.print_theMessageList();
				}
			}	
			System.out.println();
		}		
		
		//this function takes in a filepath and returns a list of messages for the filepath
		public ArrayList<Message> get_theMessageListOfFile(String filepath)
		{
			ArrayList<Message> m = null;
			
			Iterator i = _theProtocolMessageList.iterator();
			
			while(i.hasNext())
			{
				Object o = i.next();
				if(o instanceof Protocols)
				{
					Protocols current = (Protocols)o;
					if(current.get_filename().equals(filepath))
					{
						System.out.println("Found a message list for: " + filepath);
						m = current.get_messageList();
					}
				}
			}					
			return m;
		}
		
		public void transform_messageList(String filepath)		
		{
			ArrayList<Message> m = new ArrayList<Message>();
			m = null;

			m=get_theMessageListOfFile(filepath);
				
			Iterator i = m.iterator();
			while(i.hasNext())
			{
				Object o = i.next();
				if(o instanceof Message)
				{
					Message m1 = (Message)o;
					m1.set_child(get_agent(m1.get_child()));
					m1.set_parent(get_agent(m1.get_parent()));
				}
			}					
		}
		
		public String get_agent(String role)
		{
			String agent = null;
			Iterator i = _theAgentList.iterator();
			
			while(i.hasNext())
			{
				Object o = i.next();
				if(o instanceof Agent)
				{
					Agent a = (Agent)o;
					
					ArrayList agent_roles = new ArrayList();
					agent_roles=a.get_roles();
					
					for(int j=0; j<agent_roles.size(); j++)
					{
						if(agent_roles.get(j).equals(role))
						{
							agent=a.get_name();
							break;
						}
					}
				}
			}
			return agent;
		}
		
		public void print_theMessageList(ArrayList<Message> _messageList)
		{
			Iterator iterator = _messageList.iterator();
			
			System.out.println("LIST OF MESSAGES:");
			
			while(iterator.hasNext())
			{
				Object o = iterator.next();
				if(o instanceof Message)
				{
					Message m = (Message)o;
					System.out.println("MESSAGE: " + m.get_name() + " | TYPE: " + m.get_type() + " | FROM: " + m.get_parent() + " | TO: " + m.get_child());		
				}							
			}	
			System.out.println();
		}
		
		public ArrayList<Agent> get_theAgentList() {
			return _theAgentList;
		}

		public void set_theAgentList(ArrayList<Agent> agentList) {
			_theAgentList = agentList;
		}

		public ArrayList<Protocols> get_theProtocolMessageList() {
			return _theProtocolMessageList;
		}

		public void set_theProtocolMessageList(ArrayList<Protocols> protocolMessageList) {
			_theProtocolMessageList = protocolMessageList;
		}					
}
